package tongcheng.com.tongcheng.base.main.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import butterknife.Bind;
import tongcheng.com.tongcheng.R;
import tongcheng.com.tongcheng.base.BaseActivity;
import tongcheng.com.tongcheng.utils.Setting;
import tongcheng.com.tongcheng.utils.ShowToast;

public class TiXianActivity extends BaseActivity {

    @Bind(R.id.back)
    ImageView mBack;
    @Bind(R.id.title)
    TextView mTitle;
    @Bind(R.id.title_bar)
    RelativeLayout mBg;
    @Bind(R.id.tixian_ll1)
    LinearLayout mTixianLl1;
    @Bind(R.id.tixian_ok)
    Button mTixianOk;
    @Bind(R.id.rb_weiXinTiXian)
    RadioButton rb_weiXinTiXian;
    @Bind(R.id.rb_zhiFuBaoTiXian)
    RadioButton rb_zhiFuBaoTiXian;
    @Bind(R.id.et_tiXianJinE)
    EditText et_tiXianJinE;


    public int temp = 0;

  /*  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ti_xian);
        ButterKnife.bind(this);
    }
*/
    @Override
    public Integer initResid() {
        return R.layout.activity_ti_xian;
    }

    @Override
    public void initCreate(Bundle savedInstanceState) {
        settitlebar(mBg);
        mTitle.setText("提现");
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        //微信提现
        rb_weiXinTiXian.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    temp = 1;
                    rb_zhiFuBaoTiXian.setChecked(false);

                }
            }
        });

        //支付宝提现
        rb_zhiFuBaoTiXian.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(b){
                    temp = 2;
                    rb_weiXinTiXian.setChecked(false);

                }
            }
        });

        //确认提现
        mTixianOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String jine = et_tiXianJinE.getText().toString().trim();//提现金额
                double yue = Double.parseDouble(Setting.getYuE());//账户余额

                if(temp == 1){//微信
                    if(TextUtils.isEmpty(jine)){
                        ShowToast.showToast("请输入提现金额");
                    }else {
                        double num = Double.parseDouble(jine);
                        if(num <= yue && num > 0){
                            ShowToast.showToast("你选择了微信提现");
                            boolean isWeiXin = isWeixinAvilible(TiXianActivity.this);
                            if(isWeiXin){
                                //微信提现

                            }else {
                                ShowToast.showToast("你尚未安装微信");
                            }

                        }else if(num == 0){
                            ShowToast.showToast("提现金额不能为零");
                        }else if(num > yue){
                            ShowToast.showToast("可用余额不足");
                        }
                    }
                }else if(temp == 2){//支付宝
                    if(TextUtils.isEmpty(jine)){
                        ShowToast.showToast("请输入提现金额");
                    }else {
                        double num = Double.parseDouble(jine);
                        if(num <= yue && num > 0){
                            ShowToast.showToast("你选择了支付宝提现");
                            boolean bbb = checkAliPayInstalled(TiXianActivity.this);
                            if(bbb){
                                //支付宝提现
                                Intent intent = new Intent(TiXianActivity.this,ZhiFuBaoTiXianActivity.class);
                                intent.putExtra("jine",jine);
                                startActivity(intent);
                            }else {
                                ShowToast.showToast("你尚未安装支付宝");
                            }
                        }else if(num == 0){
                            ShowToast.showToast("提现金额不能为零");
                        } else if(num > yue){
                            ShowToast.showToast("可用余额不足");
                        }
                    }
                }else if(temp == 0){
                    if(TextUtils.isEmpty(jine)){
                        ShowToast.showToast("请输入提现金额");
                    }else {
                        double num = Double.parseDouble(jine);
                        if(num <= yue && num > 0){
                            ShowToast.showToast("请选择一种提现方式");
                        }else if(num == 0){
                            ShowToast.showToast("提现金额不能为零");
                        } else if(num > yue){
                            ShowToast.showToast("可用余额不足");
                        }
                    }
                }
            }
        });
    }


}
